package com.example.demo.web;

import com.alibaba.fastjson.JSON;
import com.example.demo.domain.Dynamic;
import com.example.demo.handle.HandleImage;
import com.example.demo.services.DynamicServer;
import com.example.demo.services.UserServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

@Controller
public class DynamicController {
    final static String URL="http://localhost/static/img/liu/";
    @Autowired
    UserServer userServer;
    @Autowired
    DynamicServer dynamicServer;

    @GetMapping("/dynamic")
    public String Dynamic(Model model) {
        model.addAttribute("allname", userServer.getListName());
        model.addAttribute("dynamic", new Dynamic());
        return "dynamic";
    }

    @PostMapping("/dynamic/commit")
    @ResponseBody
    public String Commit(@ModelAttribute Dynamic dynamic,@RequestParam(value = "userid") Integer userid,@RequestParam(value = "file") MultipartFile file) {
        Map<String,Object> map=new HashMap<>();
        try {
            dynamic.setUser_id(userid);
            String originalFilename = file.getOriginalFilename();

            System.out.println("originalFilename"+originalFilename);
            String formatName=originalFilename.split("\\.")[1];
            String url = ResourceUtils.getURL("classpath:").getPath()+"static/img/liu/"+originalFilename;
            File dir=new File(url);
            if (!dir.exists()) {
                boolean mkdir = dir.mkdirs();
                System.out.println(mkdir);
            }
            file.transferTo(dir);
            //处理图片
            HandleImage.changeSize(400,600,dir.getPath(),formatName);
            System.out.println(dir.getAbsolutePath());
            dynamic.setImg_url(URL+originalFilename);
            dynamic.setUser_id(userid);
            long currentTimeMillis = System.currentTimeMillis();
            dynamic.setTime_stamp(String.valueOf(currentTimeMillis));
            dynamicServer.save(dynamic);
            map.put("status", 1);
            map.put("message", "添加成功");
        } catch (Exception e) {
            map.put("status1", 0);
            map.put("message1", "添加失败");
            e.getMessage();
        }
        return JSON.toJSONString(map);
    }
    @GetMapping("/dynamic/delete_file")
    @ResponseBody
    public String delete_file(){
        try {
            String path = ResourceUtils.getURL("classpath:").getPath()+"static/img/liu";
            File file=new File(path);
            if (file.exists()){
                deleteAllFile(file);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "删除失败";
        }
        return "删除成功";
    }
    public void deleteAllFile(File file){
        if(file.isDirectory()) {
            File[] files = file.listFiles();
            for (File fil : files) {
                if (fil.isFile()) {
                    fil.delete();
                } else {
                    deleteAllFile(fil);
                    //不加这句文件夹删除不了
                    fil.delete();
                }
            }
        }
    }
    @GetMapping("/dynamic/queryAllNews")
    @ResponseBody
    public String queryAllNews(){
        return JSON.toJSONString(dynamicServer.queryAllNews());
    }
    @GetMapping(value = "/dynamic/readmore")
    @ResponseBody
    public String readmore(@RequestParam("id") Long id){
        return JSON.toJSONString(dynamicServer.getDynamicInfo(id));
    }
}
